package com.pm.buglist.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pm.buglist.domain.BugDescription;
import com.pm.buglist.mapper.BugDescriptionMapper;
import com.pm.buglist.service.BugDescriptionService;
import com.pm.common.core.domain.AjaxResult;
import com.pm.common.core.domain.entity.SysRole;
import com.pm.common.core.domain.model.LoginUser;
import com.pm.common.enums.RoleKey;
import com.pm.common.utils.SecurityUtils;
import com.pm.common.utils.StringUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * bugs描述表 服务实现类
 * </p>
 *
 * @author wwm
 * @since 2023-12-14
 */
@Service
public class BugDescriptionServiceImpl extends ServiceImpl<BugDescriptionMapper, BugDescription> implements BugDescriptionService {

    @Override
    public AjaxResult insertBugDescription(BugDescription description) {
        //获取当前登录用户信息
        LoginUser loginUser = SecurityUtils.getLoginUser();
        List<SysRole> roles = loginUser.getUser().getRoles();
        Boolean flag=false;
        if(!CollectionUtils.isEmpty(roles)){//验证当前人员是否是品质人员
            for (SysRole role : roles) {
                String roleKey = role.getRoleKey();
                if(roleKey.equals(RoleKey.DQE.getCode())|| roleKey.equals(RoleKey.DQA.getCode())){
                    flag=true;
                    break;
                }
            }
        }
        if(!flag){
            return AjaxResult.error("你无权限执行此操作");
        }
        description.setHandleUser(loginUser.getUserId());
        description.setCreateTime(LocalDateTime.now());
        description.setStatus("1");//发布的状态
        baseMapper.insert(description);
        return AjaxResult.success("添加成功");
    }

    @Override
    public AjaxResult updateBugDescription(BugDescription description) {
        //获取当前登录用户信息
        LoginUser loginUser = SecurityUtils.getLoginUser();
        List<SysRole> roles = loginUser.getUser().getRoles();
        Boolean flag=false;
        if(!CollectionUtils.isEmpty(roles)){//验证当前人员是否是品质人员
            for (SysRole role : roles) {
                String roleKey = role.getRoleKey();
                if(roleKey.equals(RoleKey.DQE.getCode())|| roleKey.equals(RoleKey.DQA.getCode())){
                    flag=true;
                    break;
                }
            }
        }
        if(!flag){
            return AjaxResult.error("你无权限执行此操作");
        }
        Integer id = description.getId();
        if(ObjectUtils.isEmpty(id)){
            return AjaxResult.error("查询的数据不能为空");
        }
        baseMapper.updateById(description);
        return AjaxResult.success("修改成功");
    }

    @Override
    public AjaxResult getHandleBugHistory(Long bid) {
        if(ObjectUtils.isEmpty(bid)){
            return AjaxResult.error("查找的数据不能为空");
        }
        List<BugDescription> bugDescriptions = baseMapper.bugDescriptionLogByBid(bid);
        //封装数据
        JSONObject jsonObject = new JSONObject();
        String descriptions="";
        String analysisAnswer="";
        for (BugDescription item : bugDescriptions) {
            String Dheader="<p><strong style='color:red'>处理人：</strong>"+item.getDhandleUserName()+"， "+"<strong style='color:red'>版本：</strong>"+item.getVersion()+"</p>";
            String Aheader="<p><strong style='color:red'>处理人：</strong>"+item.getAhandleUserName()+"， "+"<strong style='color:red'>版本：</strong>"+item.getVersion()+"</p>";
            descriptions=descriptions+Dheader+item.getDescriptions()+"<br/>";
            if(!StringUtils.isEmpty(item.getAnalysis())){
                analysisAnswer=analysisAnswer+Aheader+item.getAnalysis()+"<br/>"+item.getAnswer()+"<br/>";
            }
        }
        jsonObject.put("descriptions",descriptions);
        jsonObject.put("analysisAnswer",analysisAnswer);
        return AjaxResult.success(jsonObject);
    }

}
